/**
 * @author yxw
 * date : 2013年10月3日 下午12:40:26 
 */
package com.tzinfo.tzt.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.tzinfo.tzt.config.FilterConfig;
import com.tzinfo.tzt.R;

public class FilterContentActivity extends Activity implements
		OnItemClickListener, OnClickListener {

	private ListView mListView;
	private TextView mTextView;
	private SimpleAdapter simpleAdapter;
	private Button backBtn;

	private int tabNum = 0;

	private List<Map<String, Object>> data;

	private int filterNum, filterContent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_filter_content);
		init();
	}

	private void init() {
		Intent intent = this.getIntent();
		filterNum = intent.getIntExtra("filterNum", 0);
		tabNum = intent.getIntExtra("tabNum", 0);
		filterContent = intent.getIntExtra("filterContent", 0);

		backBtn = (Button) this.findViewById(R.id.bt_back);
		backBtn.setOnClickListener(this);
		mTextView = (TextView) this.findViewById(R.id.text_title);
		mTextView.setText(FilterConfig.FILTER_NAMES[filterNum]);
		mListView = (ListView) this.findViewById(R.id.list_content);
		data = new ArrayList<Map<String, Object>>();
		getData(filterContent);
		simpleAdapter = new SimpleAdapter(this, data,
				R.layout.item_filter_content, new String[] { "text", "img" },
				new int[] { R.id.text_filter_content, R.id.img_filter_content });
		mListView.setAdapter(simpleAdapter);
		mListView.setOnItemClickListener(this);
	}

	private void getData(int flag) {
		Log.e("tabNum", tabNum+"");
		Log.e("filterNum", filterNum+"");
		Map<String, Object> map;
		if (tabNum == 3 && filterNum == 4) {// 租房特殊处理
			String[] fiterStrs = FilterConfig.FILTER_CONTENT[filterNum];
			if(fiterStrs[fiterStrs.length - 2].length()==2){
				fiterStrs[fiterStrs.length - 2] += "及以上";
			}
			for (int i = 0; i < fiterStrs.length - 1; i++) {
				map = new HashMap<String, Object>();
				map.put("text", FilterConfig.FILTER_CONTENT[filterNum][i]);
				if (flag == i) {
					map.put("img", R.drawable.screening_icon7_0903);
				} else {
					map.put("img", null);
				}
				data.add(map);
			}
			
		} else { // 一般情况下
			if (tabNum == 2&& filterNum == 4){
				String[] fiterStrs = FilterConfig.FILTER_CONTENT[filterNum];
				if(fiterStrs[fiterStrs.length - 2].length()!=2){
					//恢复字长
					fiterStrs[fiterStrs.length - 2] = fiterStrs[fiterStrs.length - 2]
							.substring(0, fiterStrs[fiterStrs.length - 2].length() - 3);
				}
			}
			for (int i = 0; i < FilterConfig.FILTER_CONTENT[filterNum].length; i++) {
				map = new HashMap<String, Object>();
				map.put("text", FilterConfig.FILTER_CONTENT[filterNum][i]);
				if (flag == i) {
					map.put("img", R.drawable.screening_icon7_0903);
				} else {
					map.put("img", null);
				}
				data.add(map);
			}
		}
	}

	@Override
	public void onItemClick(AdapterView<?> adapterview, View view, int i, long l) {
		Intent intent = new Intent();
		data.clear();
		getData(i);
		simpleAdapter.notifyDataSetChanged();
		intent.putExtra("filterContent", i);
		intent.putExtra("filterNum", filterNum);
		setResult(2, intent);
		finish();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt_back:
			this.finish();
			break;

		default:
			break;
		}

	}

}
